/* 
 * Advanced Foundation Classes
 * Copyright (C) 2000/2004  Fabio Rotondo - Andrea Galimberti
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *  
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "menu.h"

static const char class_name [] = "EGUI Widget Menu";

static int afc_easygui_internal_menu_get_template ( DynamicClass * dyn );
static int afc_easygui_internal_cb_menu_start ( DynamicClass * dyn );
static int afc_easygui_internal_cb_menu_end ( DynamicClass * dyn );
static int afc_easygui_menu_method_add ( DynamicClass * dyn );


DynamicClass * afc_easygui_internal_menu_new_instance ()
{
	DynamicClass * dyn = NULL;

	if ( ( dyn = afc_dynamic_class_new () ) == NULL ) 
	{
		AFC_LOG_FAST ( AFC_ERR_NO_MEMORY );
		return ( NULL );
	}

	afc_dynamic_class_add_method ( dyn, "get_template", NULL,  afc_easygui_internal_menu_get_template );
	afc_dynamic_class_add_method ( dyn, "open_callback", NULL,  afc_easygui_internal_cb_menu_start );
	afc_dynamic_class_add_method ( dyn, "close_callback", NULL,  afc_easygui_internal_cb_menu_end );
	afc_dynamic_class_add_method ( dyn, "enabled",	NULL, 	afc_easygui_widget_method_enabled );

	afc_dynamic_class_add_method ( dyn, "add", NULL,  	      afc_easygui_menu_method_add );

	return ( dyn );
}

int afc_easygui_internal_menu_del_instance ( DynamicClass * dyn )
{
	return ( afc_easygui_widget_standard_free ( dyn ) );
}

static int afc_easygui_internal_menu_get_template ( DynamicClass * dyn )
{
	dyn->result = "TEXT ICON TEXTPOS";
	return ( AFC_ERR_NO_ERROR );
}

static int afc_easygui_internal_cb_menu_start ( DynamicClass * dyn )
{
	EasyGUI * egui = dyn->info;
	EasyGUIContext * context = egui->curr_context;
	GtkWidget * w;
	GtkWidget * menu_item;
	GtkWidget * box;
	char * text, * icon, * textpos;
	DynamicClass * dc;

	if ( context->curr_container == NULL ) return ( AFC_EASYGUI_ERR_NO_CONTAINER );

	afc_easygui_widget_parse_default_params ( egui );

	text 	 = afc_cmd_parser_arg_get_by_name ( egui->parser, "TEXT" );
	icon 	 = afc_cmd_parser_arg_get_by_name ( egui->parser, "ICON" );
	textpos	 = afc_cmd_parser_arg_get_by_name ( egui->parser, "TEXTPOS" );

	// This is just a "patch" to avoid empty menus
	if ( ( text == NULL ) && ( icon == NULL ) ) text = "(menu)";

	if ( ( dc = afc_dynamic_class_master_new_instance ( egui->commands, "menu" ) ) == NULL )
		return ( AFC_LOG_FAST_INFO ( AFC_ERR_NO_MEMORY, "dynclass instance" ) );


	WIDGET_SET_CONTEXT ( dc, context );

	menu_item = gtk_menu_item_new ();
	w = gtk_menu_new ();

	box = afc_easygui_widget_create_label ( dc, text, icon, afc_easygui_widget_get_horient ( context, textpos ) );

	gtk_menu_item_set_submenu ( GTK_MENU_ITEM ( menu_item ), w );
	gtk_container_add ( GTK_CONTAINER ( menu_item ), box );

	afc_dynamic_class_set_var ( dc, AFC_DYNAMIC_CLASS_VAR_KIND_POINTER, "egui_menu_item", menu_item );
	afc_dynamic_class_set_var ( dc, AFC_DYNAMIC_CLASS_VAR_KIND_POINTER, "egui_menu", w );

	afc_easygui_context_set_menu ( context, w );

	if ( egui->def_params->border ) gtk_container_set_border_width ( GTK_CONTAINER ( menu_item ), egui->def_params->border );

	return ( afc_easygui_context_add_container ( context, menu_item, dc, AFC_EASYGUI_CONTAINER_TYPE_MENU, AFC_EASYGUI_CONTAINER_HOMOGENEOUS_NONE ) );
}

static int afc_easygui_internal_cb_menu_end ( DynamicClass * dyn )
{
	EasyGUI * egui = dyn->info;

	return ( afc_easygui_context_close_container ( egui->curr_context ) );
}

// METHODS
static int afc_easygui_menu_method_add ( DynamicClass * dyn )
{
	GtkWidget * w;
	GtkWidget * menu;

	menu = afc_dynamic_class_get_var ( dyn, "egui_menu" );
	w = afc_array_master_first ( dyn->args );

	gtk_menu_shell_append ( GTK_MENU_SHELL ( menu ), w );

	return ( AFC_ERR_NO_ERROR );
}
